home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1993 / 3 / 02 / automobil / bilanz / source / source.ampk / BilanzCalc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  15.3 KB  |  555 lines

  1. #include "Bilanz.h"
  2.  
  3. void StatusMonth(long dir)    /* show infos about month */
  4. { char s[15], i;
  5.   for (i=1; i<=13; i++) {
  6.     ScrollRaster(RP,0L,dir,100L,31L,124L,43L);
  7.     ScrollRaster(RP,0L,dir,256L,31L,336L,43L);
  8.     ScrollRaster(RP,0L,dir,432L,31L,512L,43L);
  9.     Delay(1);
  10.   }
  11.   SetAPen(RP,1L);
  12.   Print(Monthname[curmonth],100,40);
  13.   ltos(monthexpend,s,10,8,' '); Print(s,256,40);
  14.   SetAPen(RP,2L);
  15.   ltos(monthincome,s,10,8,' '); Print(s,432,40);
  16. }
  17. void StatusYear(long dir)    /* show infos of the year */
  18. { char s[15];
  19.   int mon, i;
  20.   if (dir) {
  21.     for (i=1; i<=13; i++) {
  22.       ScrollRaster(RP,0L,dir,96L,14L,128L,26L);
  23.       ScrollRaster(RP,0L,dir,256L,14L,336L,26L);
  24.       ScrollRaster(RP,0L,dir,432L,14L,512L,26L);
  25.       ScrollRaster(RP,0L,dir,256L,31L,336L,43L);
  26.       ScrollRaster(RP,0L,dir,432L,31L,512L,43L);
  27.       Delay(1);
  28.     }
  29.   }
  30.   if (!filter) {
  31.     yearincome = year->income;
  32.     yearexpend = year->expend;
  33.   }
  34.   else {
  35.     struct Entry *entry;
  36.     yearincome = yearexpend = 0;
  37.     for (mon=1; mon<=12; mon++) {
  38.       entry = year->month[mon].first;
  39.       while (entry) {
  40.         if (FilterCmp(entry->area)) {
  41.           if (entry->costs > 0) yearincome += entry->costs;
  42.           else                  yearexpend += labs(entry->costs);
  43.         }
  44.         entry = entry->next;
  45.       }
  46.     }
  47.   }
  48.   SetAPen(RP,1L);
  49.   ltos(curyear,s,4,0,' '); Print(s,96,23);
  50.   Print(Monthname[curmonth],100,40);
  51.   ltos(yearexpend,s,10,8,' '); Print(s,256,23);
  52.   ltos(monthexpend,s,10,8,' '); Print(s,256,40);
  53.   SetAPen(RP,2L);
  54.   ltos(yearincome,s,10,8,' '); Print(s,432,23);
  55.   ltos(monthincome,s,10,8,' '); Print(s,432,40);
  56. }
  57.  
  58. void Arrow(void)    /* draws arrows if not all entries are shown */
  59. {
  60.   int arrowup, arrowdown;
  61.   if (!filter) {
  62.     arrowup   = (!count) ? 0 : 1;
  63.     arrowdown = (count < number-MAXLINE) ? 1 : 0;
  64.   }
  65.   else {
  66.     struct Entry *entry;
  67.     int i, j;
  68.     entry = year->month[curmonth].first;
  69.     i = 0;
  70.     while (entry && !FilterCmp(entry->area)) {
  71.       entry = entry->next;
  72.       i++;
  73.     }
  74.     arrowup = (i < count) ? 1 : 0;
  75.     j = 0;
  76.     while (entry && i<count) {
  77.       if (FilterCmp(entry->area)) j++;
  78.       entry = entry->next; i++;
  79.     }
  80.     arrowdown = (j < number-MAXLINE) ? 1 : 0;
  81.   }
  82.   if (arrowup) {
  83.     SetAPen(RP,3L);
  84.     RectFill(RP,11,101,14,107);
  85.     Move(RP,8,104); Draw(RP,17,104); Draw(RP,13,100);
  86.     Draw(RP,12,100); Draw(RP,9,103); Draw(RP,15,103);
  87.     SetAPen(RP,1L);
  88.     Move(RP,12,108); Draw(RP,15,108); Draw(RP,15,105);
  89.     Draw(RP,18,105);
  90.   }
  91.   else {
  92.     SetAPen(RP,0L);
  93.     RectFill(RP,8,100,18,108);
  94.   }
  95.   if (arrowdown) {
  96.     SetAPen(RP,3L);
  97.     RectFill(RP,11,242,14,248);
  98.     Move(RP,8,245); Draw(RP,17,245); Draw(RP,13,249);
  99.     Draw(RP,12,249); Draw(RP,9,246); Draw(RP,15,246);
  100.     SetAPen(RP,1L);
  101.     Move(RP,15,243); Draw(RP,15,244);
  102.     Move(RP,13,250); Draw(RP,17,246);
  103.     Move(RP,14,250); Draw(RP,18,246);
  104.   }
  105.   else {
  106.     SetAPen(RP,0L);
  107.     RectFill(RP,8,242,18,250);
  108.   }
  109. }
  110.  
  111. void OutClr(void)    /* clear screen */
  112. {
  113.   SetAPen(RP,0L); RectFill(RP,8L,100L,635L,250L);
  114. }
  115.  
  116. void Out(short first, short number, short offset)    /* shows entries */
  117. {
  118.   struct Entry *entry;
  119.   short  i, line;
  120.   long   costs;
  121.   char   s[100], date[7];
  122.   date[2] = date[4] = '.'; date[5] = date[6] = 0;
  123.   if (curmonth < 10) date[3] = curmonth + 48;
  124.   else   { date[3] = '1'; date[4] = curmonth + 38; date[5] = '.'; }
  125.   i = first + offset;
  126.   entry = year->month[curmonth].first;
  127.   while (i && entry) { entry = entry->next; i--; }
  128.   while (number && entry) {
  129.     if (!filter || FilterCmp(entry->area)) {
  130.       line = 10 * offset + 108;
  131.       ltos(entry->date,s,2,0,' '); date[0] = s[0]; date[1] = s[1];
  132.       if ((costs=entry->costs) > 0) SetAPen(RP,2L);
  133.       else { costs = -costs; SetAPen(RP,1L); }
  134.       ltos(costs,s,10,8,' '); Print(s,448-(modus*80),line);
  135.       SetAPen(RP,1L);
  136.       Print(date,40-(modus*8),line);
  137.       if (!modus) Print(entry->title,112,line);
  138.       else {
  139.         strcpy(s,entry->title);
  140.         if (strlen(s) > 33) {
  141.           s[30] = s[31] = s[32] = '.'; s[33] = 0;
  142.         }
  143.         Print(s,96,line);
  144.         Print(entry->area,472,line);
  145.       }
  146.       offset++; number--;
  147.     }
  148.     entry = entry->next;
  149.   }
  150.   Arrow();
  151. }
  152.  
  153.  
  154. void OutPrinter()    /* print entries on printer*/
  155. {
  156.   FILE   *fp;
  157.   struct Entry *entry;
  158.   short  prtnumber, filter2=filter;
  159.   unsigned char daypos=0, i, s[100], date[11], t[20];
  160.   StatusClr();
  161.   ltos(curyear,date,10,0,' ');
  162.   date[2] = date[3] = date[5] = '.';
  163.   if (curmonth < 10) { daypos=1; date[4] = curmonth + 48; }
  164.   else          { date[3] = '1'; date[4] = curmonth + 38; }
  165.   if (fp = fopen(prtname,"w")) {
  166.     entry = year->month[curmonth].first;
  167.     prtnumber = year->month[curmonth].number;
  168.     ltos(prtnumber,t,4,0,' '); strcpy(s,"Noch "); strcat(s,t);
  169.     strcat(s," Einträge zu drucken..."); StatusText(s,0,1);
  170.     ltos(curyear,s,4,0,' ');
  171.     fprintf(fp,"Bilanz %s %s",Monthname[curmonth],s);
  172.     if (filter2) {
  173.       fprintf(fp," für ");
  174.       for (i=0;i<FILTER;i++) {
  175.         if (filterflag[i]) {
  176.           fprintf(fp,"%s",year->filtertext[i]);
  177.           filter2 -= power2(i);
  178.           if (filter2) fprintf(fp,", ");
  179.         }
  180.       }
  181.     }
  182.     fprintf(fp,"\n\n");
  183.     ltos(yearincome,s,10,8,' '); ltos(yearexpend,t,10,8,' ');
  184.     fprintf(fp,"Jahreseinnahmen: %s     Jahresausgaben: %s\n",s,t);
  185.     ltos(monthincome,s,10,8,' '); ltos(monthexpend,t,10,8,' ');
  186.     fprintf(fp,"Monatseinnahmen: %s     Monatsausgaben: %s\n\n",s,t);
  187.     while (entry) {
  188.       if (!filter || FilterCmp(entry->area)) {
  189.         ltos(prtnumber--,t,4,0,' '); strcpy(s,"Noch "); strcat(s,t);
  190.         StatusText(s,0,1);
  191.         ltos(entry->date,s,2,0,' ');
  192.         date[daypos] = s[0]; date[daypos+1] = s[1];
  193.         ltos(entry->costs,s,10,8,' ');
  194.         fprintf(fp,"%s  %-40s %s  %s\n",date,entry->title,s,entry->area);
  195.       }
  196.       entry = entry->next;
  197.     }
  198.     fclose(fp);
  199.     StatusClr();
  200.   }
  201.   else {
  202.     strcpy(s,"Kann "); strcat(s,prtname); strcat(s," nicht öffnen!");
  203.     StatusText(s,1,2);
  204.   }
  205. }
  206.  
  207. void OutputDirection()
  208. { char change=changeflag;
  209.   OutClr(); Frame(18,110,621,148,3);
  210.   SetAPen(RP,1L); Print("Ausgabe auf ... (z.B. \"PRT:\" oder \"DF0:ausgabe\")",34,122);
  211.   ReadText(prtname,34,140,70,1,INS, stopRead, 0,1,2,1);
  212.   changeflag=change;
  213. }
  214.  
  215.  
  216. void ShowGadgets()    /* draws gadgets (|<,<,>,>|) if necessary */
  217. { if (number <= MAXLINE) {
  218.     if (entrygadgets) {
  219.       entrygadgets = 0;
  220.       Gadget("  ", 80,49,115,59,0);
  221.       Gadget(" " ,120,49,155,59,0);
  222.       Gadget(" " ,160,49,195,59,0);
  223.       Gadget("  ",200,49,235,59,0);
  224.     }
  225.   }
  226.   else if (entrygadgets<1) {
  227.     entrygadgets = 1;
  228.     Gadget("|<", 80,49,115,59,0);
  229.     Gadget("<" ,120,49,155,59,0);
  230.     Gadget(">" ,160,49,195,59,0);
  231.     Gadget(">|",200,49,235,59,0);
  232.   }
  233. }
  234.  
  235. void OutStart()    /* calculate costs and number of entries */
  236. {
  237.   OutClr(); count = 0;
  238.   if (!filter) {
  239.     number = year->month[curmonth].number;
  240.     monthincome = year->month[curmonth].income;
  241.     monthexpend = year->month[curmonth].expend;
  242.   }
  243.   else {
  244.     struct Entry *entry;
  245.     int i=0;
  246.     number = 0;
  247.     monthincome = monthexpend = 0;
  248.     entry = year->month[curmonth].first;
  249.     while (entry) {
  250.       if (FilterCmp(entry->area)) {
  251.         if (!number) count = i; /* first filtered entry */
  252.         number++;
  253.         if (entry->costs > 0) monthincome += entry->costs;
  254.         else                  monthexpend += labs(entry->costs);
  255.       }
  256.       entry = entry->next; i++;
  257.     }
  258.   }
  259.   ShowGadgets();
  260.   Out(count,MAXLINE,0);
  261. }
  262.  
  263. void GetFilename(void)    /* sets filename */
  264. { unsigned char s[5];
  265.   strcpy(filename,PATH); strcat(filename,file);
  266.   ltos(curyear,s,4,0,' '); strcat(filename,s);
  267.   strcat(filename,".dat");
  268. }
  269.  
  270. void Start()    /* draws layout */
  271. {
  272.   firstyear = 0;
  273.   changeflag = modus = filter = counter = 0;
  274.   include = 1;
  275.   entrygadgets = -1;
  276.   strcpy(prtname,PRTNAME);
  277.   Gadget("<", 60,15, 90,25,0);
  278.   Gadget(">",134,15,164,25,0);
  279.   Gadget("<", 60,32, 90,42,0);
  280.   Gadget(">",134,32,164,42,0);
  281.   Gadget("Neu",240,49,289,59,0);
  282.   Gadget("Monat drucken",360,49,480,59,0);
  283.   Gadget("Voll",584,49,629,59,modus);
  284.   Gadget("INCL",584,66,629,76,include);
  285.   Frame(8,13,520,27,3); /* for year */
  286.   Frame(8,30,520,44,3); /* for month */
  287.   Frame(8,81,631,95,3); /* for status */
  288.   SetAPen(RP,1L);
  289.   Print("Jahr" ,16,23); Print("Ausgaben:",184,23); Print("Einnahmen:",352,23);
  290.   Print("Monat",13,40); Print("Ausgaben:",184,40); Print("Einnahmen:",352,40);
  291.   Print("Eintrag",12,57); Print("Ausgabe",516,57);
  292.   Print("Filter",12,74);
  293.   Print("Status:",12,91);
  294.   curyear  = time.year;
  295.   curmonth = time.month;
  296.   if (!InsertYear(&year)) {
  297.     StatusText("Zu wenig Speicherplatz!\n",1,2);
  298.     Delay(250);
  299.     exit(1);
  300.   }
  301.   GetFilename();
  302.   LoadData(filename);
  303.   OutStart();
  304.   StatusYear(0);
  305. }
  306.  
  307. void YearChange(short step)    /* step back/forward one year */
  308. { short ok;
  309.   if (changeflag) {    /* save changed data */
  310.     SaveData(filename);
  311.     StatusClr();
  312.     changeflag = 0;
  313.   }
  314.   curyear += step;
  315.   if ((ok = InsertYear(&year)) == 0) {    /* mem for new year */
  316.     StatusText("Nicht genug Speicherplatz frei!",1,2);
  317.     curyear -= step;
  318.   }
  319.   else {
  320.     GetFilename();            /* new year includes new filename */
  321.     OutClr();
  322.     if (ok == 1) LoadData(filename);    /* load new data */
  323.     else {                /* data exists already */
  324.       if (step==1) year = year->next;
  325.       else {
  326.         year = firstyear;
  327.         while (year->year != curyear) year = year->next;
  328.       }
  329.     }
  330.     OutStart();
  331.     StatusYear(step);
  332.     UpdateFilter(-1);
  333.   }
  334. }
  335.  
  336. void MonthBack()
  337. {
  338.   if (--curmonth < 1) curmonth=12;
  339.   OutStart();
  340.   StatusMonth(-1);
  341. }
  342. void MonthNext()
  343. {
  344.   if (++curmonth > 12) curmonth=1;
  345.   OutStart();
  346.   StatusMonth(1);
  347. }
  348.  
  349. void EntryFirst()
  350. {
  351.   if (Count(0)) {
  352.     Gadget("|<",80,49,115,59,1);
  353.     OutClr(); Out(count,MAXLINE,0);
  354.   }
  355.   Gadget("|<",80,49,115,59,0);
  356. }
  357. void EntryBack()
  358. { short i;
  359.   while ((qualifier & 0x4000L || code==76) && Count(-1)) {
  360.     Gadget("<",120,49,155,59,1);
  361.     for(i=1; i<=10; i++) ScrollRaster(RP,0L,-1L,24L,100L,635L,250L);
  362.     Out(count,1,0);
  363.     GetMessageClass();
  364.   }
  365.   Gadget("<",120,49,155,59,0);
  366. }
  367. void EntryNext()
  368. { short i;
  369.   while ((qualifier & 0x4000L || code==77) && Count(1)) {
  370.     Gadget(">",160,49,195,59,1);
  371.     for(i=1; i<=10; i++) ScrollRaster(RP,0L,1L,24L,100L,635L,250L);
  372.     Out(count,1,(short)(MAXLINE-1));
  373.     GetMessageClass(); GetMessageClass();
  374.   }
  375.   Gadget(">",160,49,195,59,0);
  376. }
  377. void EntryLast()
  378. {
  379.   if (Count(2)) {
  380.     Gadget(">|",200,49,235,59,1);
  381.     OutClr(); Out(count,MAXLINE,0);
  382.   }
  383.   Gadget(">|",200,49,235,59,0);
  384. }
  385.  
  386.  
  387. void EntryInput(struct Entry *entry, int modus)
  388. { /* modus = 0 -> new entry
  389.      modus = 1 -> change or delete entry */
  390.   unsigned char s[200], abbruch=0, w, deleted = 0,
  391.            stopRead[] = {13,1,4,5,0}, stopDate[] = {13,1,2,4,5,0};
  392.   int x, y, line=1, flag=0;
  393.   float costs;
  394.   if (modus) {
  395.     SetAPen(RP,1L);
  396.     Print("Eintrag löschen",470,200);
  397.     Frame(440,185,620,209,3);
  398.   }
  399.   else {
  400.     entry->date = time.day;
  401.     entry->area[0] = entry->title[0] = 0;
  402.     entry->costs = 0;
  403.   }
  404.   SetAPen(RP,1L);
  405.   Print("Datum  :   .",64,140);
  406.    Print(Monthname[curmonth],168,140);
  407.    ltos(curyear,s,4,0,' '); Print(s,200,140);
  408.   Print("Titel  :",64,150); Print(entry->title,136,150);
  409.   Print("Bereich:",64,160); Print(entry->area,136,160);
  410.   Print("Betrag :",64,170); ltos(entry->costs,s,10,8,' '); Print(s,136,170);
  411.   Print("Ok",310,200); Frame(280,185,356,209,3);
  412.   if (entry->costs > 0) {
  413.     year->month[curmonth].income -= entry->costs;
  414.     year->income -= entry->costs;
  415.     if (!filter || FilterCmp(entry->area)) monthincome -= entry->costs;
  416.   }
  417.   else {
  418.     year->month[curmonth].expend += entry->costs;
  419.     year->expend += entry->costs;
  420.     if (!filter || FilterCmp(entry->area)) monthexpend += entry->costs;
  421.   }
  422.   do {
  423.     switch (line) {
  424.     case 1: ltos(entry->date,s,2,0,' ');
  425.             w = ReadText(s,136,140,2,1,OVR,stopDate,0,1,2,1);
  426.             entry->date = atol(s);
  427.             if (entry->date < 0 || entry->date > 31) entry->date = 0;
  428.             ltos(entry->date,s,2,0,' '); Print(s,136,140);
  429.             line = (w==65) ? 5 : 2;
  430.             break;
  431.     case 2: w = ReadText(entry->title,136,150,40,1,INS,stopRead,0,1,2,1);
  432.             line = (w==65) ? 1 : 3;
  433.             break;
  434.     case 3: w = ReadText(entry->area,136,160,20,1,INS,stopRead,0,1,2,1);
  435.             line = (w==65) ? 2 : 4;
  436.             break;
  437.     case 4: if (entry->costs==0) strcpy(s,"0         ");
  438.             else ltos(entry->costs,s,10,8,' ');
  439.             w = ReadText(s,136,170,10,1,OVR,stopRead,0,1,2,1);
  440.             costs = round(stof(s) * 100.0);
  441.         if (costs <= -1e8 || costs >= 1e8) {
  442.           StatusText("Betrag muß > -1 Millionen und < +1 Millionen sein",0,2);
  443.           costs = 0.0;
  444.             }
  445.         entry->costs = costs;
  446.             ltos(entry->costs,s,10,8,' '); Print(s,136,170);
  447.             line = (w==65) ? 3 : 5;
  448.             break;
  449.     case 5: strcpy(s,"Ok");
  450.             w = ReadText(s,310,200,2,1,INS,stopRead,2,1,2,1);
  451.             Print("Ok",310,200);
  452.             if (w==13) abbruch=1;
  453.             line = (w==65) ? 4 : 1;
  454.     }
  455.     if (w>=48 && w<=57) {
  456.       w-=48;
  457.       strcpy(entry->area,year->filtertext[w]);
  458.       SetAPen(RP,0L); RectFill(RP,136L,152L,300L,161L);
  459.       SetAPen(RP,1L); Print(entry->area,136,160);
  460.       changeflag = 1; line = 4;
  461.     }
  462.     else if (w==1) {
  463.       x = win->MouseX; y = win->MouseY;
  464.       if (y > 64 && y < 78) {
  465.         flag = (x - 64)/74;
  466.         if (flag>=0 && flag<FILTER) {
  467.           FilterSwitch(flag);
  468.           flag++;
  469.         }
  470.       }
  471.       do {
  472.         GetMessageClass();
  473.       } while (qualifier & 0x4000L);
  474.       x = win->MouseX; y = win->MouseY;
  475.       if (flag) {
  476.         strcpy(entry->area,year->filtertext[--flag]);
  477.         SetAPen(RP,0L); RectFill(RP,136L,152L,300L,161L);
  478.         SetAPen(RP,1L); Print(entry->area,136,160);
  479.         changeflag = 1; line = 4;
  480.         FilterSwitch(flag);
  481.         flag = 0;
  482.       }
  483.       else if (y>=185 && y<=209) {
  484.         if (x>=280 && x<=356) abbruch = 1;
  485.         else if (x>=440) {
  486.           DeleteEntry(entry);
  487.           changeflag = 1; deleted = abbruch = 1;
  488.         }
  489.         else line = 5;
  490.       }
  491.       else {
  492.         line = (y - 122) / 10;
  493.         if (line < 1) line = 1;
  494.         else if (line > 4) line = 5;
  495.       }
  496.     }
  497.   } while (!abbruch);
  498.   if (deleted) {
  499.     year->month[curmonth].number--;
  500.     if (!filter || FilterCmp(entry->area)) number--;
  501.     Count(-1);
  502.   }
  503.   else if (entry->costs > 0) {
  504.     year->month[curmonth].income += entry->costs;
  505.     year->income += entry->costs;
  506.     if (!filter || FilterCmp(entry->area)) monthincome += entry->costs;
  507.   }
  508.   else {
  509.     year->month[curmonth].expend -= entry->costs;
  510.     year->expend -= entry->costs;
  511.     if (!filter || FilterCmp(entry->area)) monthexpend -= entry->costs;
  512.   }
  513. }
  514.  
  515.  
  516. void EntryNew()
  517. { struct Entry entry;
  518.   int    change = changeflag;
  519.   OutClr();
  520.   SetAPen(RP,1L); Print("Neuer Eintrag: ",50,120);
  521.   changeflag = 0;
  522.   EntryInput(&entry,0);
  523.   if (changeflag) {
  524.     if (InsertEntry(entry,curmonth)) {
  525.       year->month[curmonth].number++;
  526.       if (!filter || FilterCmp(entry.area)) number++;
  527.       StatusYear(1);
  528.       ShowGadgets();
  529.       Count(2);
  530.     }
  531.     else StatusText("Nicht genug Speicherplatz!",0,2);
  532.   }
  533.   else changeflag = change;
  534.   OutClr(); Out(count,MAXLINE,0);
  535. }
  536.  
  537. void EntryChange(int nr)
  538. { struct Entry *entry;
  539.   int i;
  540.   OutClr();
  541.   SetAPen(RP,1L); Print("Eintrag ändern:",50,120);
  542.   entry = year->month[curmonth].first;
  543.   for (i=0; i<count; i++) entry = entry->next;
  544.   i = 0;
  545.   while (i < nr) {
  546.     entry = entry->next;
  547.     if (!filter) i++;
  548.     else if (FilterCmp(entry->area)) i++;
  549.   }
  550.   EntryInput(entry,1);
  551.   StatusYear(1);
  552.   ShowGadgets();
  553.   OutClr(); Out(count,MAXLINE,0);
  554. }
  555.